cmake_minimum_required(VERSION 3.15)
project(ovip VERSION 0.0.1)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include)

# install
set(CMAKE_INSTALL_PREFIX /usr/local)
set(CMAKE_INSTALL_LOCAL_ONLY TRUE)

if(NOT SIMULATOR)
	set(SIMULATOR "verilaotr")
endif(NOT SIMULATOR)

if(SIMULATOR STREQUAL "verilator")
	execute_process(
		COMMAND
			bash -c
			"verilator -V|grep ROOT|grep verilator|awk '{print $3}'|xargs echo -n"
		OUTPUT_VARIABLE V_ROOT)

	if(V_ROOT)
		message(STATUS "Find verilator root: ${V_ROOT}")
	else()
		message(
			FATAL_ERROR
				"Cannot find verilator, please installl (Verilator 4.218 2022-01-17 rev v4.218)")
	endif()
    include_directories(${V_ROOT}/include ${V_ROOT}/include/vltstd)
    message(STATUS "Verilator include path: ${V_ROOT}/include ${V_ROOT}/include/vltstd")
elseif(SIMULATOR STREQUAL "vcs")
	if(DEFINED ENV{VCS_HOME})
		set(VCS_HOME $ENV{VCS_HOME})
		message(STATUS "Find VCS root: ${VCS_HOME}")
	else()
		message(
			FATAL_ERROR
				"Cannot find vcs, please install (vcs/O-2018.09-SP2) or set \$\{VCS_HOME\}"
		)
	endif()

	# Add verilator include path for build
	include_directories(${VCS_HOME}/include)
    message(STATUS "VCS include path: ${VCS_HOME}/include")
endif()

if(${OVIP_WASM_LIB})
set(XSPCOMM_WASM_LIB TRUE)
endif()

string(TOLOWER "$ENV{BUILD_OVIP_SWIG}" swig_lang)
if("${swig_lang}" STREQUAL "javascript")
set(XSPCOMM_WASM_LIB TRUE)
endif()

add_subdirectory(xcomm)
init_xspcomm()

add_subdirectory(src/comm)
add_subdirectory(src/third)
add_subdirectory(swig)

add_custom_target(sed_ENABLE_OVIP_XCOROUTINE ALL
VERBATIM
COMMAND ${CMAKE_COMMAND} -E copy_directory
							"${CMAKE_CURRENT_SOURCE_DIR}/include"
							"${CMAKE_CURRENT_BINARY_DIR}/include"
COMMAND sed -i "s/ENABLE_XCOROUTINE/${ENABLE_XCOROUTINE}/g" ${CMAKE_CURRENT_BINARY_DIR}/include/openvip/tilelink.h
COMMAND sed -i "s/ENABLE_XCOROUTINE/${ENABLE_XCOROUTINE}/g" ${CMAKE_CURRENT_BINARY_DIR}/include/openvip_third/xs_coupledl2.h
COMMAND sed -i "s/ENABLE_XCOROUTINE/${ENABLE_XCOROUTINE}/g" ${CMAKE_CURRENT_BINARY_DIR}/include/openvip_third/nutshell_simpleram.h
DEPENDS ${PROJECT_NAME}
)

add_dependencies(${PROJECT_NAME} copy_xspcomm_include)

install(TARGETS ${PROJECT_NAME} ovip_third LIBRARY DESTINATION lib)
install(DIRECTORY ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/ DESTINATION include)
